package com.xuanzuo.birthday.helper;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

import com.xuanzuo.birthday.model.Friend;
import com.xuanzuo.birthday.service.Birthday;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;

public class FriendDataHelper {
    //数据库版本
    private static int DB_VERSION = 2;
    private SQLiteDatabase db;
    private FriendSqliteHelper dbHelper;
    
    public FriendDataHelper(Context context){
        dbHelper=new FriendSqliteHelper(context, dbHelper.TB_NAME, null, DB_VERSION);
        db= dbHelper.getWritableDatabase();
    }
    
    public void close()
    {
        db.close();
        dbHelper.close();
    }
    
    //获取friends表中的FriendID、Access Token、Access Secret的记录
    public List<Friend> getFriendList(Boolean isSimple)
    {
        List<Friend> friendList = new ArrayList<Friend>();
        Cursor cursor=db.query(dbHelper.TB_NAME, null, null, null, null, null, Friend.ID+" DESC");
        cursor.moveToFirst();
        while(!cursor.isAfterLast()&& (cursor.getString(1)!=null)){
            Friend friend=new Friend();
            friend.setId(Integer.parseInt(cursor.getString(0)));
            friend.setFriendid(cursor.getString(1));
            if(!isSimple){
            	friend.setFriendname(cursor.getString(2));
	            ByteArrayInputStream stream = new ByteArrayInputStream(cursor.getBlob(3)); 
	            Drawable icon= Drawable.createFromStream(stream, "image");
	            friend.setIcon(icon);
            }
            friend.setMediatype(cursor.getString(4));
            friend.setStatus(cursor.getString(5));
            friend.setBirthday(cursor.getString(6));
            friend.setWishes(cursor.getString(7));
            friend.setAlarmtime(cursor.getString(8));
            friend.setAlarmtype(cursor.getString(9));
            friendList.add(friend);
            cursor.moveToNext();
        }
        cursor.close();
        return friendList;
    }
    
    //判断friends表中的是否包含某个FriendID的记录
    public Boolean haveFriend(String friendId, String mediaType)
    {
        Boolean b=false;
        Cursor cursor=db.query(dbHelper.TB_NAME, null, Friend.FRIENDID + "=" + friendId + " and "+Friend.MEDIATYPE + "=" +Birthday.getInstance().getSelectedMedia(), null, null, null,null);
        b=cursor.moveToFirst();
        Log.e("HaveFriendInfo",b.toString());
        cursor.close();
        return b;
    }

    //添加friends表的记录
    public Long saveFriendInfo(Friend friend)
    {
        ContentValues values = new ContentValues();
        values.put(Friend.FRIENDID, friend.getFriendid());
        values.put(Friend.FRIENDNAME, friend.getFriendname());

//        Drawable icon = friend.getIcon();
        
//        values.put(Friend.ICON, friend.getIcon());
        values.put(Friend.FRIENDID, friend.getFriendid());
        values.put(Friend.ALARMTIME, friend.getAlarmtime());
        values.put(Friend.ALARMTYPE, friend.getAlarmtype());
        values.put(Friend.BIRTHDAY, friend.getBirthday());
        values.put(Friend.MEDIATYPE, friend.getMediatype());
        values.put(Friend.STATUS, friend.getStatus());
        values.put(Friend.WISHES, friend.getWishes());
        
        Long uid = db.insert(dbHelper.TB_NAME, Friend.ID, values);
        Log.e("SaveFriendInfo",uid+"");
        return uid;
    }
    
    //更新friends表的记录，根据FriendId更新用户昵称和用户图标
    public int updateIcon(String FriendId, Bitmap friendIcon)
    {
        ContentValues values = new ContentValues();
        // BLOB类型  
        final ByteArrayOutputStream os = new ByteArrayOutputStream();  
        // 将Bitmap压缩成PNG编码，质量为100%存储          
        friendIcon.compress(Bitmap.CompressFormat.PNG, 100, os);   
        // 构造SQLite的Content对象，这里也可以使用raw  
        values.put(Friend.ICON, os.toByteArray());
        int id= db.update(dbHelper.TB_NAME, values, Friend.FRIENDID + "=" + FriendId, null);
        Log.e("UpdateFriendInfo2",id+"");
        return id;
    }
    
    //更新friends表的记录
    public int updateFtriendInfo(Friend friend)
    {
        ContentValues values = new ContentValues();
        values.put(Friend.FRIENDID, friend.getFriendid());
        
        int id= db.update(dbHelper.TB_NAME, values, Friend.ID + "=" + friend.getId(), null);
        Log.e("UpdateFriendInfo",id+"");
        return id;
    }
    
    //删除friends表的记录
    public int delFriendInfo(String FriendId){
        int id=  db.delete(dbHelper.TB_NAME, Friend.FRIENDID +"="+FriendId, null);
        Log.e("DelFriendInfo",id+"");
        return id;
    }
    
    public Friend getFriendByName(String friendName){
		Friend friend = new Friend();
		try{
	    	Cursor cursor = db.rawQuery("select * from "+dbHelper.TB_NAME+" where "+Friend.FRIENDNAME+"=?", new String[]{friendName});
	    	if(cursor.getCount() > 0 && cursor.moveToFirst()){
	    		int id = cursor.getInt(0);
	    		String friendid = cursor.getString(1);
	    		friendName = cursor.getString(2);
	            ByteArrayInputStream stream = new ByteArrayInputStream(cursor.getBlob(3)); 
	            Drawable icon= Drawable.createFromStream(stream, "image");
	            String mediaType = cursor.getString(4);
	            String status = cursor.getString(5);
	            String birthday = cursor.getString(6);
	            String wishes = cursor.getString(7);
	            String alarmtime = cursor.getString(8);
	            String alarmtype = cursor.getString(9);
	            friend.setAlarmtime(alarmtime);
	            friend.setAlarmtype(alarmtype);
	            friend.setBirthday(birthday);
	            friend.setFriendid(friendid);
	            friend.setFriendname(friendName);
	            friend.setIcon(icon);
	            friend.setId(id);
	            friend.setMediatype(mediaType);
	            friend.setStatus(status);
	            friend.setWishes(wishes);
	    	}
	    	cursor.close();
	    	return friend;
		}catch (Exception e) {
			return null;
		}
    }
    

    public Friend getFriendById(String idstr){
    	Friend friend = new Friend();
		try{
	    	Cursor cursor = db.rawQuery("select * from "+dbHelper.TB_NAME+" where "+Friend.ID+"=?", new String[]{idstr});
	    	if(cursor.getCount() > 0 && cursor.moveToFirst()){
	    		int id = cursor.getInt(0);
	    		String friendid = cursor.getString(1);
	    		String friendName = cursor.getString(2);
	            ByteArrayInputStream stream = new ByteArrayInputStream(cursor.getBlob(3)); 
	            Drawable icon= Drawable.createFromStream(stream, "image");
	            String mediaType = cursor.getString(4);
	            String status = cursor.getString(5);
	            String birthday = cursor.getString(6);
	            String wishes = cursor.getString(7);
	            String alarmtime = cursor.getString(8);
	            String alarmtype = cursor.getString(9);
	            friend.setAlarmtime(alarmtime);
	            friend.setAlarmtype(alarmtype);
	            friend.setBirthday(birthday);
	            friend.setFriendid(friendid);
	            friend.setFriendname(friendName);
	            friend.setIcon(icon);
	            friend.setId(id);
	            friend.setMediatype(mediaType);
	            friend.setStatus(status);
	            friend.setWishes(wishes);
	    	}
	    	cursor.close();
	    	return friend;
		}catch (Exception e) {
			return null;
		}
    }
}
